package generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * <p>
 * 测试生成代码
 * </p>
 */
public class CodeGeneratorTemp {

    @Test
    public void generateCode() {
        String packageName = "com.example";
        //user -> UserService, 设置成true: user -> IUserService
        generateByTables(packageName, "session_registry");
    }

    private void generateByTables(String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        config.setOutputDir("D:\\mybatis-plus-codeGen");
        config.setActiveRecord(true);
        config.setFileOverride(true);
        config.setBaseResultMap(true);// XML ResultMap
        config.setEnableCache(false);
        config.setBaseColumnList(false);
        config.setAuthor("author");
        config.setMapperName("%sMapper");
        config.setXmlName("%sMapper");
        config.setServiceName("MP%sService");
        config.setServiceImplName("%sServiceImpl");
        config.setControllerName("%sController");// XML columList;
//        config.setServiceName("%sService");

        //数据库配置
        String dbUrl = "jdbc:mysql://129.211.25.169:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        String driverName = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "123456";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName(driverName);
        dataSourceConfig.setUrl(dbUrl);
        dataSourceConfig.setUsername(userName);
        dataSourceConfig.setPassword(password);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(false);
        strategyConfig.setEntityLombokModel(true);
//        strategyConfig.setDbColumnUnderline(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityColumnConstant(true);
        //strategyConfig.setSuperEntityClass("com.rockemb.autogeneration.model.SupperModel")
        //修改替换成你需要的表名，多个表名传数组
        strategyConfig.setInclude(tableNames);

        PackageConfig config1 = new PackageConfig();
        config1.setParent(packageName);
        config1.setMapper("dao");
        config1.setService("service");
        config1.setController("controller");
        config1.setEntity("model");

        TemplateConfig tc = new TemplateConfig();
        tc.setController("/templates/controller.java.vm");
//        tc.setController(null);
//        tc.setMapper(null);
//        tc.setService(null);
//        tc.setServiceImpl(null);
//        tc.setEntity(null);
//        tc.setEntity("/templates/entity.java");
        tc.setXml("/templates/mapper.xml.vm");

        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(config);
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setPackageInfo(config1);
        autoGenerator.setTemplate(tc);

        autoGenerator.execute();
    }
}